package com.xh.bussiness.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.system.bean.request.PaperTemplateRequest;
import com.xh.bussiness.system.dao.PaperTemplateMapper;
import com.xh.bussiness.system.model.PaperTemplate;
import com.xh.bussiness.system.service.IPaperTemplateService;
import com.xh.core.bean.XhPage;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;

@Service
public class PaperTemplateServiceImpl extends BaseServiceImpl<PaperTemplateMapper, PaperTemplate> implements IPaperTemplateService {

    @Override
    public XhPage queryPaperTemplatePage(XhPage page, PaperTemplateRequest bean) {
        QueryWrapper qw=new QueryWrapper<PaperTemplate>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List<PaperTemplate> queryBySubjectCode(String subjectCode,Integer type) {
        QueryWrapper<PaperTemplate> qw = new QueryWrapper<>();
        qw.eq("subject_code", subjectCode);
        if (type != null) {
            qw.eq("type", type);
        }
        return this.dao.selectList(qw);
    }

    @Override
    public void batchChangeStatus(String[] ids, Integer status) {
        List<PaperTemplate> paperTemplates = super.getDao().selectBatchIds(Arrays.asList(ids));
        if (!CollectionUtils.isEmpty(paperTemplates)) {
            for (PaperTemplate paperTemplate : paperTemplates) {
                paperTemplate.setStatus(status);
            }
            super.batchUpdate(paperTemplates);
        }
    }

    private QueryWrapper prepareParams(PaperTemplateRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if (!StringUtils.isEmpty(bean.getSubjectCode())) {
            qw.eq("subject_code", bean.getSubjectCode());
        }
        if (bean.getType() != null) {
            qw.eq("type", bean.getType());
        }
        qw.orderByDesc("create_time");
        return qw;
    }
}
